// LSL script generated: RLV-New.Chat Replacement.replace.lslp Sun May  2 00:09:51 Westeuropäische Sommerzeit 2010
integer attachment = 1;
string cmdname;
key victim;
list gMenu = [];
list statesMenu = [0,0,0,0];
key ToucherID;
integer channel_dialog;
integer listen_id;
integer listen_id2;
integer listen_id3;
integer listen_id4;
integer listen_id5;
integer listen_id6;
string msg = "Hover Test";
list sideMenu = [" ","Back"," "];
list gEmote = [];
list gChat = [];
string wearer;
string objektname;

string gsCardName = "Dv_Boots";
list glCardRows;
key gkQuery;
integer giLine = 0;

llRlvSay(string command){
    if ((attachment == 1)) {
        llOwnerSay(("@" + command));
    }
    else  {
        llSay(-1812221819,(((((cmdname + ",") + ((string)victim)) + ",") + "@") + command));
    }
}

MakeRLVState(){
    if ((attachment == 1)) {
        if ((llList2Integer(statesMenu,0) == 1)) llRlvSay("redirchat:123457=add");
        if ((llList2Integer(statesMenu,1) == 1)) llRlvSay("rediremote:123456=add");
        if ((llList2Integer(statesMenu,2) == 1)) llRlvSay("sendim_sec=n");
        if ((llList2Integer(statesMenu,3) == 1)) llRlvSay("recvim_sec=n");
        if ((gChat == [])) {
            (glCardRows = []);
            (gsCardName = "Chat");
            (giLine = 0);
            (gkQuery = llGetNotecardLine(gsCardName,giLine));
        }
    }
}

llLRP(integer element_new,integer placeinlist){
    (statesMenu = llListReplaceList(statesMenu,[element_new],placeinlist,placeinlist));
}

makeMenu(){
    (gMenu = []);
    (gMenu += sideMenu);
    if ((llList2Integer(statesMenu,0) == 1)) (gMenu += "[x]Chat");
    else  (gMenu += "[ ]Chat");
    if ((llList2Integer(statesMenu,1) == 1)) (gMenu += "[x]Emote");
    else  (gMenu += "[ ]Emote");
    if ((llList2Integer(statesMenu,2) == 1)) (gMenu += "[x]Read IM");
    else  (gMenu += "[ ]Read IM");
    if ((llList2Integer(statesMenu,3) == 1)) (gMenu += "[x]Send IM");
    else  (gMenu += "[ ]Send IM");
    (gMenu += ["Name","Add Chat","Add Emote","Reset"]);
    (channel_dialog = (-1 * ((integer)("0x" + llGetSubString(((string)llGetKey()),-5,-1)))));
    (listen_id = llListen(channel_dialog,"",NULL_KEY,""));
    llDialog(ToucherID,msg,gMenu,channel_dialog);
    llSetTimerEvent(45.0);
}

default {

    state_entry() {
        (wearer = llKey2Name(llGetOwner()));
        (objektname = llGetObjectName());
        if ((gChat == [])) {
            (glCardRows = []);
            (gsCardName = "Chat");
            (giLine = 0);
            (gkQuery = llGetNotecardLine(gsCardName,giLine));
        }
    }

    
    dataserver(key _query_id,string _data) {
        if ((_query_id == gkQuery)) {
            if ((_data == EOF)) {
                if ((gsCardName == "Chat")) {
                    (gChat = glCardRows);
                    (glCardRows = []);
                    (gsCardName = "Emote");
                    (giLine = 0);
                    (gkQuery = llGetNotecardLine(gsCardName,giLine));
                }
                else  if ((gsCardName == "Emote")) {
                    (gEmote = glCardRows);
                    (glCardRows = []);
                }
            }
            else  {
                (glCardRows = (glCardRows + _data));
                (++giLine);
                (gkQuery = llGetNotecardLine(gsCardName,giLine));
            }
        }
    }

    
    listen(integer channel,string name,key id,string message) {
        if ((channel == channel_dialog)) {
            llListenRemove(listen_id);
            llSetTimerEvent(0.0);
            if ((message == "[x]Chat")) {
                llLRP(0,0);
                llRlvSay("redirchat:123457=rem");
                llListenRemove(listen_id2);
                makeMenu();
            }
            if ((message == "[ ]Chat")) {
                llLRP(1,0);
                llRlvSay("redirchat:123457=add");
                (listen_id2 = llListen(123457,"",llGetOwner(),""));
                makeMenu();
            }
            if ((message == "[x]Emote")) {
                llLRP(0,1);
                llRlvSay("rediremote:123456=rem");
                llListenRemove(listen_id3);
                makeMenu();
            }
            if ((message == "[ ]Emote")) {
                llLRP(1,1);
                llRlvSay("rediremote:123456=add");
                (listen_id3 = llListen(123456,"",llGetOwner(),""));
                makeMenu();
            }
            if ((message == "[x]Read IM")) {
                llLRP(0,2);
                llRlvSay("recvim_sec=y");
                makeMenu();
            }
            if ((message == "[ ]Read IM")) {
                llLRP(1,2);
                llRlvSay("recvim_sec=n");
                makeMenu();
            }
            if ((message == "[x]Send IM")) {
                llLRP(0,3);
                llRlvSay("sendim_sec=y");
                makeMenu();
            }
            if ((message == "[ ]Send IM")) {
                llLRP(1,3);
                llRlvSay("sendim_sec=n");
                makeMenu();
            }
            if ((message == "Name")) {
                (listen_id4 = llListen(13,"",ToucherID,""));
                llInstantMessage(ToucherID,"Change name: /13 <Name>");
            }
            if ((message == "Add Chat")) {
                (listen_id5 = llListen(14,"",ToucherID,""));
                llInstantMessage(ToucherID,"Add Chat: /14 <Text>");
            }
            if ((message == "Add Emote")) {
                (listen_id6 = llListen(15,"",ToucherID,""));
                llInstantMessage(ToucherID,"Add Emote: /15 <Text>");
            }
            if ((message == "Reset")) {
                (gChat = []);
                (gEmote = []);
                if ((gChat == [])) {
                    (glCardRows = []);
                    (gsCardName = "Chat");
                    (giLine = 0);
                    (gkQuery = llGetNotecardLine(gsCardName,giLine));
                }
            }
            if ((message == "Back")) llMessageLinked(-4,1337,"DV_MAIN_MISC",ToucherID);
        }
        if ((channel == 123457)) {
            float grand = llFrand((llGetListLength(gChat) - 1));
            integer random = llRound(grand);
            llSetObjectName(wearer);
            string saying = llList2String(gChat,random);
            llSay(0,saying);
            llSetObjectName(objektname);
        }
        if ((channel == 123456)) {
            float grand = llFrand((llGetListLength(gEmote) - 1));
            integer random = llRound(grand);
            llSetObjectName(wearer);
            string saying = llList2String(gEmote,random);
            llSay(0,("/me " + saying));
            llSetObjectName(objektname);
        }
        if ((channel == 13)) {
            llListenRemove(listen_id4);
            (wearer = message);
        }
        if ((channel == 14)) {
            llListenRemove(listen_id5);
            (gChat += message);
        }
        if ((channel == 15)) {
            llListenRemove(listen_id6);
            (gEmote += message);
        }
    }

    
    link_message(integer sender,integer num,string str,key id) {
        if ((num == 1337)) {
            if ((str == llGetScriptName())) {
                llMessageLinked(-4,1338,llGetScriptName(),NULL_KEY);
                (ToucherID = id);
                makeMenu();
            }
        }
        if ((num == 1338)) {
            if ((str == "attachment")) (attachment = 1);
            else  if ((str == "device")) (attachment = 0);
            else  if ((str != llGetScriptName())) {
                llListenRemove(listen_id);
                llListenRemove(listen_id4);
                llListenRemove(listen_id5);
                llListenRemove(listen_id6);
            }
        }
        if ((num == 1339)) {
            if ((str == "ON_REZ")) {
                MakeRLVState();
            }
            if ((str == "RESET")) {
                (statesMenu = [0,0,0,0]);
            }
        }
        if ((num == 1329)) {
            (victim = ((key)str));
            (cmdname = ((string)llGetKey()));
        }
    }

  	
  	timer() {
        llSetTimerEvent(0.0);
        llListenRemove(listen_id);
    }
}
